# -*- coding: utf-8 -*-
from flask import current_app
from celery_tasks import celery
from common.api.sms_api import SmsApi


@celery.task(bind=True, name='sms.send_msg', retry_backoff=3)
def async_sms(self, phone, msg, **kwargs):
    if phone:
        try:
            flag, msg = SmsApi().send_message(msg=msg, phone=phone)
        except Exception as e:
            current_app.logger.error(f'【短信发送失败】{e}')
            raise self.retry(exc=e, max_retries=3)

        if not flag:
            current_app.logger.error(f'【短信发送失败】{msg}')
            raise self.retry(exc=Exception(msg), max_retries=3)
